package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.study.model.pojo.Colle;
import com.study.model.pojo.Form;
import com.study.model.pojo.FormsDetail;
import com.study.services.FormService;

/**
 * 集客控制层
 * @author 郭星
 *
 */
@Controller
public class FormsController {

	@Autowired
	FormService formService;
	
	
	
	/*@RequestMapping("")
	public String index(){
		//如果返回是void，默认访问的页面是WEB-INF/jsp/admin.jsp
		return "product";
	}*/

	/**
	 * 访问地址http://localhost:8080/huokeones
	 * @return 
	 *
	 */
	@RequestMapping("huokeones")
	public String first(HttpSession session) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("集客");
		//无返回，默认请求webapp/WEB-INF/jsp/huokeone.jsp 但是类注解必须是@RestController 
		List<Form> selectAllForm = (List<Form>) formService.selectAllForm();
		List<Colle> selectForDictionry = formService.selectForDictionry();
		
		session.setAttribute("formTitle", selectAllForm);
		session.setAttribute("formRightItem", selectForDictionry);
		return "getCustomer/huokeone";
	}
	
	/**
	 * 跳转到第二个获客界面
	 * 访问地址http://localhost:8080/huoketwoformse?forid=1
	 * @return 
	 */
	@RequestMapping("huoketwoformse")
	public String toHuokeTwo(Integer forid,String titles,HttpSession session) {
		List<FormsDetail> selectByForid = formService.selectByForid(forid);
		List<Colle> collelist = new ArrayList<>();
		System.out.println("此时的集客记录信息"+selectByForid);

	    //未处理、总记录数、处理数
		int a=0,b=0,c=0;
		int lv=0;
		if (selectByForid!=null&&selectByForid.size()>0) {
			for (FormsDetail fd:selectByForid) {
				List<Colle> colles = fd.getColles();
				//System.out.println("此主题拥有"+fd.getColles().get(0));
				if (colles!=null&&colles.size()>0) {
					for (Colle colle : colles) {
						//System.out.println(colle);
						if (colle.getDictionary2().getDicId()==1711) {
							a++;
						}else if (colle.getDictionary2().getDicId()==1712) {
							c++;
						}
						b++;
					}
					for(int i=colles.size()-1;i>=0;i--) {
						collelist.add(colles.get(i));
					}
				}
				
			}
			
			if (a!=0) {
				lv = b/a*100;
			}
		}
		
		 
		session.setAttribute("forids", forid);
		session.setAttribute("formTwoItem", selectByForid );
		session.setAttribute("formBottomItem", collelist );
		//session.setAttribute("notgive", a );
		session.setAttribute("yesgive", b );
		session.setAttribute("havegive", c );
		session.setAttribute("titles", titles);
		//session.setAttribute("lvs", lv );
		System.out.println("跳转到第二个获客界面："+a+"  "+b+"  "+lv+" "+c);

		//ModelAndView mv= new ModelAndView("getCustomer/huoketwoform");
		return "getCustomer/huoketwoform";
	}
	
	
	/**
	 * 跳转到第三个获客界面
	 * 访问地址http://localhost:8080/huokethreehtml?forid=1
	 * 传入那个表单详情的id
	 * @return 
	 * @return 
	 */
	@RequestMapping("/huokethreehtml")
	public ModelAndView toHuokehuokethreehtml(Integer forid,Integer uid,HttpSession session) {
		session.setAttribute("detailegetid", forid );
		session.setAttribute("uid", uid );
		
		//修改表单详情浏览量
		formService.updateFormDetailsRorrow(forid);
		ModelAndView mv= new ModelAndView("getCustomer/huokethreehtml5");
		mv.addObject("uid", uid);
		mv.addObject("texts", formService.findhtml(forid));
		return mv;
	}
	
	
	
	/**
	 * 添加收集到的数据
	 * 访问地址http://localhost:8080/huokeformtoadd
	 * @return 
	 * @return 
	 */
	@RequestMapping(value="/huokeformtoadd", method=RequestMethod.POST)
	public String formHuoke(String photo,String name,String needs,String males,Integer uids,HttpSession session) {
		int forditailid = (int) session.getAttribute("detailegetid");
		Integer uid = uids;
		int forid = (int) session.getAttribute("forids");
		//int forditailid = 1;
		//int uid = 2;
		//int forid = (int) session.getAttribute("forids");
		System.out.println("电话："+photo+"  姓名："+name+"  需求："+needs+"  称呼："
		+males+" 表单详情id:"+forditailid+" 员工id:"+uid);
		
		//添加集客信息
		formService.addColle(photo, name, needs, males, forditailid, uid);
		//修改表单详情表信息
		formService.updateFormDetails(forditailid, uid);
		//修改表单表信息
		formService.updateForm(forid);
		return "redirect:/huoketwoformse?forid="+forid;
	}
	
	
	/**
	 * 添加html5界面
	 * 
	 * 访问地址http://localhost:8080/huokeformtoadd
	 * @return 
	 * @return 
	 */
	@RequestMapping(value="/addhtml", method=RequestMethod.POST)
	public String addhtml(String texts,String titles,HttpSession session) {
		System.out.println("添加html5界面"+texts+"html標題："+titles);
		formService.addHtml(texts, titles);
		return "1001";
	}
	
}
